package com.ibm.moa.outmail.view;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ibm.moa.outmail.R;

public class FujianListAdapter extends BaseAdapter{
	private ViewHolder holder;
	private Context context;
	private ArrayList<String> mailList;
	public FujianListAdapter(Context context, ArrayList<String> mailList){
		this.context = context;
		this.mailList = mailList;
	}

	@Override
	public int getCount() {
		return mailList.size();
	}

	@Override
	public Object getItem(int position) {
		return mailList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.fujian_delete_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.fujian_filename);
			holder.delete = (ImageView) convertView.findViewById(R.id.fujian_delete_bt);
			holder.icon = (ImageView) convertView.findViewById(R.id.fujian_icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		final String fujianName = mailList.get(position);
		setFujianIcon(holder.icon, fujianName);
		holder.name.setText(fujianName);
		holder.delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder dialog= new AlertDialog.Builder(context)
				.setTitle("删除提示")
				.setMessage("确定删除附件："+fujianName)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						mailList.remove(position);
						FujianListAdapter.this.notifyDataSetChanged();
						dialog.dismiss();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				dialog.show();
			}
		});
		return convertView;
	}
	
	class ViewHolder {
		TextView name;
		ImageView delete;
		ImageView icon;
	}
	
	protected void setFujianIcon(ImageView fujianIcon, String fujianStr) {
		int lastIndex = fujianStr.lastIndexOf(".");
		if(lastIndex != -1 && lastIndex < fujianStr.length()){
			String fileType = fujianStr.substring(lastIndex+1).toLowerCase();
			if("doc".equals(fileType)|| "docx".equals(fileType)){
				fujianIcon.setImageResource(R.drawable.w);
			}else if("xls".equals(fileType)|| "xlsx".equals(fileType)){
				fujianIcon.setImageResource(R.drawable.x);
			}else if("ppt".equals(fileType)|| "pptx".equals(fileType)){
				fujianIcon.setImageResource(R.drawable.ppt);
			}else if("pdf".equals(fileType)){
				fujianIcon.setImageResource(R.drawable.pdf);
			}else if("txt".equals(fileType)){
				fujianIcon.setImageResource(R.drawable.txt);
			}else if("rar".equals(fileType)|| "zip".equals(fileType)){
				fujianIcon.setImageResource(R.drawable.zip);
			}else if("jpg".equals(fileType)|| "png".equals(fileType)|| "gif".equals(fileType)|| "jpeg".equals(fileType)){
				fujianIcon.setImageResource(R.drawable.jpg);
			}else{
				fujianIcon.setImageResource(R.drawable.fujian_icon);
			}
		}else{
			fujianIcon.setImageResource(R.drawable.fujian_icon);
		}
	}

}
